package controllers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;


import models.Area;
import models.Benefit;
import models.Customer;
import models.Location;
import models.RatingNom;
import models.RatingBest;
import models.RatingRegion;
import models.Site;
import models.SolutionSubmission;

@SuppressWarnings("serial")
public class CommonConnect {
	boolean hasEntries;
	private int iROWS;
	
	public CommonConnect(){
		hasEntries = false;
		iROWS = -1;
	}
	public boolean hasEntries(){
		return hasEntries;
	}
	
		
	
	
	
	public List<HashMap<String,Object>> getRegion() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
    	try {
    		Objectify ofy = ObjectifyService.begin();
    		Query<Location> q = ofy.query(Location.class).order("LocationRegionName");
    		
    		
    		for ( Location f : q.fetch() ) {
    			
    	            String strtmp = f.getLocationRegionName();    	          
    	            boolean Flg =false;
    	            int i = 0;
    	            while ( i < objList.size() ) {
                    	String o = (String) objList.get(i).get("Region");                       
                    	if (o.equals(strtmp))
                    	{
                    		Flg =true;
                    	}
                    	i ++;
    	            }
    	            if (!Flg){
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("Region", strtmp);
    				objList.add(itemMap);
    	            }
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	return objList;	
	}
	
	
	
	public List<HashMap<String,Object>> getRegionOfLocation() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
    	try {
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
					itemMap.put("Region", "Americas");
					objList.add(itemMap);
					
					itemMap = new HashMap<String,Object>();
					itemMap.put("Region", "China");
					objList.add(itemMap);
					
					itemMap = new HashMap<String,Object>();
					itemMap.put("Region", "Pacific Rim");
					objList.add(itemMap);
					
					itemMap = new HashMap<String,Object>();
					itemMap.put("Region", "South-East Asia");
					objList.add(itemMap);
					
					itemMap = new HashMap<String,Object>();
					itemMap.put("Region", "Europe");
					objList.add(itemMap);
					
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	return objList;	
	}
	
	
	public List<HashMap<String,Object>> getYearBestRegion() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
    	try {
    		Objectify ofy = ObjectifyService.begin();
    		Query<RatingRegion> q = ofy.query(RatingRegion.class).filter("isActive", true);
    		
    		
    		for ( RatingRegion f : q.fetch() ) {
    			
    	            String strtmp = f.getBestOfRegionYear();    	          
    	            boolean Flg =false;
    	            int i = 0;
    	            while ( i < objList.size() ) {
                    	String o = (String) objList.get(i).get("BestYearRegion");                       
                    	if (o.equals(strtmp))
                    	{
                    		Flg =true;
                    	}
                    	i ++;
    	            }
    	            if (!Flg){
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("BestYearRegion", strtmp);
    				objList.add(itemMap);
    	            }
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	return objList;	
	}
	
	

	public List<HashMap<String,Object>> getYearRatingBest() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
    	try {
    		Objectify ofy = ObjectifyService.begin();
    		Query<RatingBest> q = ofy.query(RatingBest.class).filter("isActive", true);;
    		for ( RatingBest f : q.fetch() ) {
    			
    	            String strtmp = f.getBestOfBestYear() ;
    	            boolean Flg =false;
    	            int i = 0;
    	            while ( i < objList.size() ) {
                    	String o = (String) objList.get(i).get("BestYear");
                    	if (o.equals(strtmp))
                    	{
                    		Flg =true;
                    	}
                    	i ++;
    	            }
    	            if (!Flg){
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("BestYear", strtmp);
    				objList.add(itemMap);
    	            }
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	return objList;	
	}
	
	
	
	
	public List<HashMap<String,Object>> getMonthOfYear() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
    	try {
    		Objectify ofy = ObjectifyService.begin();
    		Query<RatingNom> q = ofy.query(RatingNom.class);
    		for ( RatingNom f : q.fetch() ) {
    			
    	            String strtmp = f.getNominateSolutionMonth() + " "+  f.getNominateSolutionYear() ;
    	            boolean Flg =false;
    	            int i = 0;
    	            while ( i < objList.size() ) {
                    	String o = (String) objList.get(i).get("MonthOfYear");
                    	if (o.equals(strtmp))
                    	{
                    		Flg =true;
                    	}
                    	i ++;
    	            }
    	            if (!Flg){
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("MonthOfYear", strtmp);
    				objList.add(itemMap);
    	            }
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	return objList;	
	}
	
	
	

	public List<HashMap<String,Object>> GetLocation() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		iROWS = -1;
    	try {
    		
    		Objectify ofy = ObjectifyService.begin();
    		Query<Location> q = ofy.query(Location.class).order("LocationName");
    		for ( Location f : q.fetch() ) {
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
        			itemMap.put("LocationID", f.getLocID());
        			itemMap.put("LocationCode", f.getLocationCode());
    				itemMap.put("LocationIndex", f.getLocationIndex());
    				itemMap.put("LocationName", f.getLocationName());
    				itemMap.put("LocationRegionID", f.getLocationRegionID());
    				itemMap.put("LocationRegionName", f.getLocationRegionName());
    				itemMap.put("submittedBY", f.getSubmittedBY());
    				itemMap.put("submittedON", f.getSubmittedON());
    				
    				objList.add(itemMap);
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public List<HashMap<String,Object>> GetArea() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();

		iROWS = -1;
    	try {

    	    Objectify ofy = ObjectifyService.begin();
		    Query<Area> q = ofy.query(Area.class).order("areaName");
    		for ( Area f : q.fetch() ) {
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("AreaID", f.getAreaID());
    				itemMap.put("AreaName", f.getAreaName());
    				itemMap.put("SubmitBy", f.getSubmittedBY());
    				objList.add(itemMap);
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	
	public List<HashMap<String,Object>> getCustomer() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
    	try {
    		Query<Customer> q = ofy.query(Customer.class).order("custName");
    		
    	
    	
    		for ( Customer f : q.fetch() ) {
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("CustID", f.getCustID());
    				itemMap.put("CustName", f.getCustName());
    				itemMap.put("CustIndex", f.getCustIndex());
    				itemMap.put("Create", f.getSubmittedBY());
    				objList.add(itemMap);
    		}
    		
    		//============================
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	
	
	public List<HashMap<String,Object>> GetBenefit() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();

		iROWS = -1;
    	try {

    	    Objectify ofy = ObjectifyService.begin();
		    Query<Benefit > q = ofy.query(Benefit .class).order("benefitName");
    		for ( Benefit  f : q.fetch() ) {
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    				itemMap.put("BenefitID", f.getBenefitID());
    				itemMap.put("BenefitName", f.getBenefitName());
    				itemMap.put("SubmitBy", f.getSubmittedBY());
    				objList.add(itemMap);
    		}
    		
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	

	public int getRows(){
		return iROWS;
	}
	
}